package com.k.kpp.util.rpc.netty;


import javax.xml.bind.*;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

/**
 * Xml utils
 * RI JAXB
 * @author l23910
 */
public class XMLUtils {
    private XMLUtils () {
    }

    /**
     * A 转义 XML
     * @param xmlStr xmlStr
     * @return escapeXML 转义后的XML
     */
    public static String escape(String xmlStr) {
        return xmlStr.replace("<", "&lt;")
                .replace(">", "&gt;")
                .replace("'", "&apos;")
                .replace("\"", "&quot;");
    }

    /**
     * A 去除转义 XML
     * @param escapeXML escapeXML
     * @return xmlStr 去除转义的XML
     */
    public static String unescape(String escapeXML) {
        return escapeXML.replace("&lt;","<")
                .replace("&gt;",">")
                .replace("&apos;","'")
                .replace("&quot;","\"");
    }

    /**
     * xml to object
     * @param clazz clazz
     * @param xmlStr xmlStr
     * @return object
     * @throws JAXBException e
     */
    public static Object xmlToObj(Class<?> clazz, String xmlStr) throws JAXBException{
        JAXBContext jc = JAXBContext.newInstance(clazz);
        Unmarshaller u = jc.createUnmarshaller();
        return u.unmarshal( new StreamSource( new StringReader( xmlStr ) ) );
    }

    /**
     * xml to object
     * @param clazz clazz
     * @param xmlStr xmlStr
     * @param encoding encode
     * @return object
     * @throws JAXBException e
     * @throws UnsupportedEncodingException e1
     */
    public static <T> T xmlToObj(Class<T> clazz,String xmlStr,String encoding) throws JAXBException, UnsupportedEncodingException {
        JAXBContext jc = JAXBContext.newInstance(clazz);
        Unmarshaller unMar = jc.createUnmarshaller();
        ByteArrayInputStream bais=new ByteArrayInputStream(xmlStr.getBytes(encoding));
        return (T) unMar.unmarshal(bais);
    }

    /**
     * object to xmlStr
     * @param clazz clazz
     * @param object object
     * @return xmlStr
     * @throws JAXBException e
     */
    public static String objToXml(Class<?> clazz, Object object) throws JAXBException {
        return XMLUtils.objToXml(clazz, object, true);
    }

    /**
     * object to xmlStr
     * @param clazz clazz
     * @param object object
     * @param isFormattedOutput true or false
     * @return xmlStr
     * @throws JAXBException e
     */
    public static String objToXml(Class<?> clazz, Object object , boolean isFormattedOutput) throws JAXBException{
        JAXBContext context = JAXBContext.newInstance(clazz);
        Marshaller mar = context.createMarshaller();
        mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isFormattedOutput);
        mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        StringWriter writer = new StringWriter();
        mar.marshal(object, writer);
        return writer.toString();
    }
    public static String objToXmlWithoutAnno(Class<?> clazz, Object object,String root,boolean isFormat) throws JAXBException {
        JAXBElement<?> jaxbElement = new JAXBElement( new QName("", root), clazz, object);
        JAXBContext context = JAXBContext.newInstance(clazz);
        Marshaller mar = context.createMarshaller();

        // format XML
        mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,isFormat);

        // 去掉生成XML的默认报文头
        mar.setProperty(Marshaller.JAXB_FRAGMENT,true);

        // 设置编码格式utf-8
        mar.setProperty(Marshaller.JAXB_ENCODING,"utf-8");


        StringWriter writer = new StringWriter();
        mar.marshal(jaxbElement, writer);
        return writer.toString();
    }

    public static void main(String[] args) {
       /* String xmlStr = "&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;" +
                "&lt;RTFMSG&gt;" +
                "&lt;Type&gt;3&lt;/Type&gt;" +
                "&lt;Brief&gt;.jpg&lt;/Brief&gt;" +
                "&lt;Size&gt;595284&lt;/Size&gt;" +
                "&lt;Content&gt;Hydrangeas.jpg&lt;/Content&gt;" +
                "&lt;Thumbnai&gt;&lt;/Thumbnai&gt;" +
                "&lt;Authgrant&gt;&lt;/Authgrant&gt;" +
                "&lt;Url&gt;/http/sms/2019030816/2019-03-08-16-21-41_sms_bdf3bf1da3405725be763540d6601144.jpg&lt;/Url&gt;" +
                "&lt;TimeSpan&gt;&lt;/TimeSpan&gt;" +
                "&lt;/RTFMSG&gt;";
        String unescapeXML = XMLUtils.unescape(xmlStr);
        try {
            Object obj = XMLUtils.xmlToObj(Rtfmsg.class,unescapeXML);
            Rtfmsg vo = (Rtfmsg) obj;
            System.out.println("xml->vo:"+vo);
        } catch (JAXBException e) {
            e.printStackTrace();
        }*/

    }

}